package user;

import book.Book;
import book.Library;
import utils.ScannerSingleton;

import java.time.LocalDate;
import java.util.Scanner;

public class AdminUser extends User{

    public Scanner scanner = null;
    public Library library = null;
    public AdminUser(String name, int userID) {
        super(name, userID, "管理员");
        scanner = ScannerSingleton.getScanner();
        library = Library.getLibrary();
    }

    @Override
    public int display() {
        System.out.println("管理员 " + getName() + " 的操作菜单：");
        System.out.println("1.查找图书");
        System.out.println("2.打印所有图书");
        System.out.println("3.退出系统");
        System.out.println("4.上架图书");
        System.out.println("5.更新图书");
        System.out.println("6.下架图书");
        System.out.println("7.统计借阅次数");
        System.out.println("8.查看最受欢迎的前n本书");
        System.out.println("9.查看看库存状态");
        System.out.println("10.检查超过一年未下架的图书");
        System.out.println("请选择你的操作：");
        return scanner.nextInt();
    }
    //上架图书
    public void addBook() {
        System.out.println("Admin类addBook方法执行了......");
        scanner.nextLine();
        System.out.println("请输入书名：");
        String title = scanner.nextLine();
        System.out.println("请输入作者：");
        String author = scanner.nextLine();
        System.out.println("请输入类别：");
        String catagory = scanner.nextLine();
        System.out.println("请输入出版年份：");
        int year = scanner.nextInt();
        scanner.nextLine(); //吞掉换行符
        LocalDate shelfDate = LocalDate.now();

        //创建新书对象
        Book newBook = new Book(title, author, catagory, year, shelfDate);

        //调用图书类 addBook方法 添加图书
        library.addBook(newBook);
    }
    //图书修改 支持修改书名 作者 类别
    public void updateBook() {
        library.displayBooks();
        System.out.println("请输入要修改的图书id：");
        int bookID = scanner.nextInt();
        //吞掉换行符
        scanner.nextLine();
        //获取对应图书
        Book book = library.searchBookByID(bookID);
        if (book == null) {
            System.out.println("没有ID为：" + bookID + " 的书籍！");
            return;
        }
        System.out.println("当前书名为：" + book.getTitle());
        System.out.println("请输入新的书名：");
        String newTitle = scanner.nextLine();
        System.out.println("当前作者：" + book.getAuthor());
        System.out.println("请输入新的作者：");
        String newAuthor = scanner.nextLine();
        System.out.println("当前类别：" + book.getCategory());
        System.out.println("请输入新的类别：");
        String newCatagory = scanner.nextLine();
        System.out.println("当前出版年份：");
        System.out.println("请输入新的出版年份：");
        int newPublishYear = scanner.nextInt();
        scanner.nextLine();//吞掉换行符
        //更新对应书籍的信息
        book.setTitle(newTitle);
        book.setAuthor(newAuthor);
        book.setCategory(newCatagory);
        book.setPublishYear(newPublishYear);

        library.updateBook(book);
    }
    //删除书籍
    public void removeBook() {
        library.displayBooks();
        System.out.println("请输入要删除的图书ID：");
        int bookId = scanner.nextInt();
        scanner.nextLine();//吞掉换行符
        Book removeBook = library.searchBookByID(bookId);
        if (removeBook == null) {
            System.out.println("没有你要删除的图书！");
            return;
        }
        library.removeBook(removeBook.getBookID());
        System.out.println("图书：" + removeBook.getTitle() + " 已被删除！");
    }
    //查看每本书的借阅次数
    public void borrowCount() {
        library.borrowCount();
    }
    //查看最受欢迎的前n本书
    public void generateBook() {
        System.out.println("请输入你要查看的最受欢迎的前n本书，注意n值不能超过：" + library.getBookCount());
        int n = scanner.nextInt();
        scanner.nextLine();
        if (n <= 0 || n > library.getBookCount()) {
            System.out.println("没有最受欢迎的前" + n + "本书！");
            return;
        }
        library.generateBook(n);
    }
    //查看库存状态
    public void checkInventoryStatus() {
        library.checkInventory();
    }
    //查看并移除上架超过一年的图书
    public void checkAndRemoveOldBooks() {
        library.checkAndRemoveOldBook();
    }
}
